// pages/rank/rank.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
     titles:[
       "最强大脑，等你来战",
       "可能我不是第一，但是解决你还是没问题的",
       "最强大脑，等你来战",
       "最强大脑，等你来战",
       "最强大脑，等你来战",
     ],
     indicatorDots: false,
     autoplay: true,
     interval: 6000,
     duration: 1000,
     circular: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      imageUrl: app.globalData.imageUrl
    })
  },
  onShow:function(){
    getUserInfos(this)
    getBannder(this)
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    var _num = Math.floor(Math.random() * that.data.titles.length);
    console.log(_num)
    var _title = that.data.titles[_num];
    return {
      title: '' + _title,
      path: '/pages/index/index?id=' + wx.getStorageSync("userid"),
    }
  }
})


function getUserInfos(that){
  wx.getUserInfo({
    success: function (res) {
      wx.setStorageSync('userInfo', res.userInfo)//将用户信息储存在本
      wx.request({
        url: app.globalData.allUrl + "/login/saveInfo",
        data: {
          user_id: wx.getStorageSync("userid"),
          user_name: res.userInfo.nickName,
          image_url: res.userInfo.avatarUrl,
        },
        header: { 'content-type': "application/x-www-form-urlencoded" },
        method: 'POST',
        success: function (datas) {
        //  console.log(datas.data)
          getRank(that);
        }, fail: function () {
        }
      })
    }, fail: function () {
      wx.showModal({
        title: '提示',
        content: '您点击了拒绝授权,将无法正常显示个人排名信息,点击确定重新获取授权。',
        success: function (res) {
          if (res.confirm) {
            wx.openSetting({
              success: (res) => {
                if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                }
              }, fail: function (res) {
              }
            })
          }
        }
      })
    }
  })
}

function getRank(that){
  wx.request({
    url: app.globalData.allUrl + "/login/rank_list",
    data: {
      user_id:wx.getStorageSync("userid")
    },
    header: { 'content-type': "application/x-www-form-urlencoded" },
    method: 'POST',
    success: function (data) {
      var headRank=[]
      if (data.data.rank_list){
        for(let i=0;i<3;i++){
          headRank.push(data.data.rank_list[i])
        }
        if (data.data.time=="23:59:59"){
          data.data.time=false
        }
        for (let j = 0; j < data.data.rank_list.length;j++){
          if (data.data.rank_list[j].time&&data.data.rank_list[j].time<18000){
            data.data.rank_list[j].rank="我是神！"
          } else if (18000 <=data.data.rank_list[j].time&&data.data.rank_list[j].time < 20000) {
            data.data.rank_list[j].rank = "我是神！"
          } else if (20000 <= data.data.rank_list[j].time&&data.data.rank_list[j].time < 22000) {
            data.data.rank_list[j].rank = "王者五"
          } else if (22000 <= data.data.rank_list[j].time&&data.data.rank_list[j].time < 24000) {
            data.data.rank_list[j].rank = "王者四"
          } else if (24000 <= data.data.rank_list[j].time&&data.data.rank_list[j].time < 26000) {
            data.data.rank_list[j].rank = "王者三"
          } else if (26000 <= data.data.rank_list[j].time&&data.data.rank_list[j].time < 28000) {
            data.data.rank_list[j].rank = "王者二"
          } else if (28000 <= data.data.rank_list[j].time&&data.data.rank_list[j].time < 30000) {
            data.data.rank_list[j].rank = "王者一"
          } else if (30000 <= data.data.rank_list[j].time&&data.data.rank_list[j].time < 35000) {
            data.data.rank_list[j].rank = "大师"
          } else if (35000 <= data.data.rank_list[j].time && data.data.rank_list[j].time < 40000) {
            data.data.rank_list[j].rank = "钻石"
          }  else if (40000 <= data.data.rank_list[j].time&&data.data.rank_list[j].time < 45000) {
            data.data.rank_list[j].rank = "白金"
          } else if (45000 <= data.data.rank_list[j].time && data.data.rank_list[j].time < 50000) {
            data.data.rank_list[j].rank = "黄金"
          } else if (50000 <= data.data.rank_list[j].time&&data.data.rank_list[j].time < 60000) {
            data.data.rank_list[j].rank = "白银"
          } else if (60000 <= data.data.rank_list[j].time&&data.data.rank_list[j].time < 180000) {
            data.data.rank_list[j].rank = "青铜"
          } else if (180000 <= data.data.rank_list[j].time&&data.data.rank_list[j].time) {
            data.data.rank_list[j].rank = "聚乙烯^-^"
          } 
        } 
        that.setData({
          ranks: data.data.rank_list,
          headRank: headRank,
          my_rank: data.data.my_rank,
          my_time: data.data.time,
        })
      }
    }, fail: function () {
    }
  })
}

function getBannder(that) {
  wx.request({
    url: app.globalData.allUrl + "/login/image_rank",
    data: {
    },
    header: { 'content-type': "application/x-www-form-urlencoded" },
    method: 'POST',
    success: function (res) {
      // console.log(res.data)
      that.setData({
        imageUrls: res.data
      })
    }
  })
}